/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.digitaldiary.bean;

import com.digitaldiary.entity.DiaryUser;
import javax.annotation.PostConstruct;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ManagedProperty;
import javax.faces.bean.SessionScoped;
import javax.faces.context.FacesContext;

/**
 *
 * @author palash
 */
@ManagedBean(name = "homeHandler")
@SessionScoped
public class HomeHandler {
   

    DiaryUser currentLoggedInUser;

    public DiaryUser getCurrentLoggedInUser() {
        return currentLoggedInUser;
    }

    public void setCurrentLoggedInUser(DiaryUser currentLoggedInUser) {
        this.currentLoggedInUser = currentLoggedInUser;
    }

    // all methods are given below
    @PostConstruct
    public void init() {
        
    }
    
    public DiaryUser getLoggedInUser(){
        System.out.println("thiss is test");
        FacesContext context = FacesContext.getCurrentInstance();
        LoginHandler login = (LoginHandler) context.getExternalContext().getSessionMap().get("loginHandler");
        if (login != null && login.user != null) {
            this.currentLoggedInUser = login.user;
        } else {
            String loginPage = "faces/index.xhtml";
            try {
                context.getExternalContext().redirect(loginPage);
            } catch (Throwable ex) {
            }
        }
        return currentLoggedInUser;
    }
    
}
